//
//  ImagePreViewController.swift
//  AutoPrint
//
//  Created by David on 2017/1/18.
//  Copyright © 2017年 fatcat. All rights reserved.
//

import UIKit

class ImagePreViewController: UIViewController, UIScrollViewDelegate {

    var scrollView: UIScrollView!
    var imageView: UIImageView!
    
    var navBarHidden = false
    var navBar: UINavigationBar!
    var toolbarBottom: UIView!
    
    var imgName: String?
    
    var preViewImg:UIImage?
    var fullImgURL: NSURL?
    
    var localImg: UIImage?
    var localImgPath: String?
    
    var submitBtn: UIButton!
    
    var md5: String?
    
    convenience init(named: String?, preView: UIImage?, imgURL: NSURL?){
        self.init()
        imgName = named
        preViewImg = preView
        fullImgURL = imgURL
    }
    
    convenience init(named: String?, localImg: UIImage?, localFilePath: String?){
        self.init()
        imgName = named
        self.localImg = localImg
        localImgPath = localFilePath
    }
    
    override func preferredStatusBarStyle() -> UIStatusBarStyle {
        if navBarHidden {
            return .Default
        }
        return .LightContent
    }

    
    override func viewDidLoad() {
        super.viewDidLoad()

        navigationController?.setNavigationBarHidden(true, animated: true)
        
        // Do any additional setup after loading the view.
        scrollView = UIScrollView(frame: view.frame)
        scrollView.backgroundColor = UIColor.blackColor()
//        scrollView.alwaysBounceHorizontal = true
//        scrollView.alwaysBounceVertical = true
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.minimumZoomScale = 1.0
        scrollView.maximumZoomScale = 2.0
        scrollView.bouncesZoom = true
        scrollView.userInteractionEnabled = true
        scrollView.delegate = self
        view.addSubview(scrollView)
        
        imageView = UIImageView(frame: view.frame)
        imageView.backgroundColor = UIColor.blackColor()
        imageView.contentMode = .ScaleAspectFit
        scrollView.addSubview(imageView)
        imageView.userInteractionEnabled = true
        let tapGes = UITapGestureRecognizer(target: self, action: #selector(ImagePreViewController.imgTap))
        scrollView.addGestureRecognizer(tapGes)
        let doubleTapGes = UITapGestureRecognizer(target: self, action: #selector(ImagePreViewController.imgDoubleTap))
        doubleTapGes.numberOfTapsRequired = 2
        tapGes.requireGestureRecognizerToFail(doubleTapGes)
        scrollView.addGestureRecognizer(doubleTapGes)
        
        if localImg != nil {
            imageView.image = localImg!
        }else if preViewImg != nil {
            imageView.image = preViewImg!
        }
        
        navBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 64))
        navBar.barStyle = .BlackTranslucent
        navBar.tintColor = UIColor.whiteColor()
        var navTitle = ""
        if imgName != nil {
            navTitle = imgName!
        }
        let navItem = UINavigationItem(title: navTitle)
        navItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "navbar_back"), style: .Plain, target: self, action: #selector(ImagePreViewController.goBack))
        navBar.pushNavigationItem(navItem, animated: true)
        view.addSubview(navBar)
        
        
        
        toolbarBottom = UIView(frame: CGRect(x: 0, y: view.frame.height - 44, width: view.frame.width, height: 44))
        toolbarBottom.backgroundColor = UIColor(white: 0, alpha: 0.7)
        
        view.addSubview(toolbarBottom)
        
        submitBtn = UIButton(frame: CGRect(x: 0, y: 8, width: 108, height: 28))
        submitBtn.center.x = view.center.x
        submitBtn.layer.borderColor = UIColor.whiteColor().CGColor
        submitBtn.layer.borderWidth = 1
        submitBtn.layer.cornerRadius = 4
        submitBtn.setTitle("上传打印", forState: .Normal)
        submitBtn.titleLabel?.font = UIFont.systemFontOfSize(14)
        submitBtn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        toolbarBottom.addSubview(submitBtn)
        submitBtn.addTarget(self, action: #selector(ImagePreViewController.submit), forControlEvents: .TouchUpInside)
        
        
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        navigationController?.setNavigationBarHidden(true, animated: false)
        if localImgPath != nil{
            imageView.image = UIImage(contentsOfFile: localImgPath!)
        }else if fullImgURL != nil{
            
        }
    }
    
    func imgTap(){
        navBarHidden = !navBarHidden
        
        var toolbarAlpha: CGFloat = 0.0
        if !navBarHidden {
            toolbarAlpha = 1.0
        }
        
        UIView.animateWithDuration(0.3, animations: { 
            self.toolbarBottom.alpha = toolbarAlpha
            self.navBar.alpha = toolbarAlpha
        }){(finished) ->Void in
            self.setNeedsStatusBarAppearanceUpdate()
            
        }
    }
    func imgDoubleTap(){
        if scrollView.zoomScale > 1.0{
            scrollView.setZoomScale(1.0, animated: true)
        }else{
            scrollView.setZoomScale(2.0, animated: true)
        }
        
    }

    func goBack(){
        navigationController?.setNavigationBarHidden(false, animated: false)
        navigationController?.popViewControllerAnimated(true)
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.interactivePopGestureRecognizer?.delegate = nil
    }
    
    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        navigationController?.setNavigationBarHidden(false, animated: false)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        return imageView
    }
    
    func submit(){
        var fileToSubmit: UDFile?
        
        if localImgPath != nil {
            let sizeToShow = Fatcat.getFileSize(localImgPath!)
            
            fileToSubmit = UDFile(filePath: localImgPath, fileName: imgName, fileSize: sizeToShow, fileMD5: nil)
        }else if fullImgURL != nil {
            
            fileToSubmit = UDFile(filePath: fullImgURL?.absoluteString, fileName: imgName, fileSize: "在线文件", fileMD5: md5)
        }
        if fileToSubmit != nil {
            navigationController?.setNavigationBarHidden(false, animated: false)
            navigationController?.pushViewController(FileReadyViewController(files: [fileToSubmit!], step0VC: self), animated: true)
        }
        
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
